package com.jimo.ads;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;

import com.jimo.ads.databinding.ItemViewBinding;
import com.qq.e.ads.cfg.VideoOption;
import com.qq.e.ads.nativ.ADSize;
import com.qq.e.ads.nativ.NativeExpressAD;
import com.qq.e.ads.nativ.NativeExpressADView;
import com.qq.e.comm.util.AdError;

import java.util.List;

public class ListAdapter extends ArrayAdapter<ItemBean> {
    private final Activity context;
    private final String TAG="emmmmmm信息流：";
    public ListAdapter(@NonNull Activity context, int resource, @NonNull List<ItemBean> objects) {
        super(context, resource, objects);
        this.context=context;
    }
    public View getView(int position, View convertView, ViewGroup parent){
        ItemBean ItemBean = getItem(position);
        @SuppressLint("ViewHolder") ItemViewBinding itemMeBinding= ItemViewBinding.inflate(LayoutInflater.from(context));
        NativeExpressAD nativeExpressAD = new NativeExpressAD(context, new ADSize(340, ADSize.AUTO_HEIGHT), "8074451638705774", new NativeExpressAD.NativeExpressADListener() {
            @Override
            public void onADLoaded(List<NativeExpressADView> list) {
                Log.i(TAG, "onADLoaded: ");
                NativeExpressADView view = list.get(0);
                itemMeBinding.itemContainer.addView(view);
                view.render();
            }
            @Override
            public void onRenderFail(NativeExpressADView nativeExpressADView) {
                Log.i(TAG, "onRenderFail: ");
            }

            @Override
            public void onRenderSuccess(NativeExpressADView nativeExpressADView) {
                Log.i(TAG, "onRenderSuccess: ");
            }

            @Override
            public void onADExposure(NativeExpressADView nativeExpressADView) {
                Log.i(TAG, "onADExposure: ");
            }

            @Override
            public void onADClicked(NativeExpressADView nativeExpressADView) {
                Log.i(TAG, "onADClicked: ");
            }

            @Override
            public void onADClosed(NativeExpressADView nativeExpressADView) {
                Log.i(TAG, "onADClosed: ");
            }

            @Override
            public void onADLeftApplication(NativeExpressADView nativeExpressADView) {
                Log.i(TAG, "onADLeftApplication: ");
            }

            @Override
            public void onNoAD(AdError adError) {
                Log.i(TAG, "onNoAD: "+adError.getErrorMsg());
                Log.i(TAG, "onNoAD: "+adError.getErrorCode());
            }
        }); // 传入Activity
        // 注意：如果您在平台上新建平台模板广告位时，选择了支持视频，那么可以进行个性化设置（可选）
        nativeExpressAD.setVideoOption(new VideoOption.Builder()
                .setAutoPlayPolicy(VideoOption.AutoPlayPolicy.ALWAYS) // WIFI 环境下可以自动播放视频
                .setAutoPlayMuted(true) // 自动播放时为静音
                .build()); //
        nativeExpressAD.loadAD(1);
       return  itemMeBinding.getRoot();
    }
}
